package edu.towson.cosc490.incaseofemergency;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import edu.towson.cosc490.incaseofemergency.objects.Constants;
import edu.towson.cosc490.incaseofemergency.objects.SupplyKitData;

public class ItemDetailAdd extends Activity {
	private SupplyKitData supplyDB;
	
	private EditText itemNameEdit;
	private EditText amountOwnedEdit;
	private EditText amountRequiredEdit;
	private CheckBox perishableCheckBox;
	private DatePicker expirationDatePicker;
	private LinearLayout datePickerLayout;
	private int expirationDateVisibility;
	private Spinner kitTypeSpinner;
	private Button addItemButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_detail_add);
		initLayout();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.item_detail_add, menu);
		return true;
	}
	
	public void initLayout(){
		supplyDB = new SupplyKitData(this);
		itemNameEdit = (EditText) findViewById(R.id.editTextNewItemAddName);
		amountOwnedEdit = (EditText) findViewById(R.id.editTextNewItemAddOwned);
		amountRequiredEdit = (EditText) findViewById(R.id.editTextNewItemAddRequired);
		perishableCheckBox = (CheckBox) findViewById(R.id.checkBoxAddNewItemExpiration);
		kitTypeSpinner = (Spinner) findViewById(R.id.spinnerKitType);
		expirationDatePicker = (DatePicker) findViewById(R.id.datePickerAddNewItem);
		datePickerLayout = (LinearLayout) findViewById(R.id.layoutAddNewItemExpirationDial);
		perishableCheckBox.setChecked(true);
		expirationDateVisibility = View.VISIBLE;
		datePickerLayout.setVisibility(expirationDateVisibility);
		perishableCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
				if (isChecked){
					expirationDateVisibility = View.VISIBLE;
					datePickerLayout.setVisibility(expirationDateVisibility);
				} else{
					expirationDateVisibility = View.GONE;
					datePickerLayout.setVisibility(expirationDateVisibility);
				}
			}
		});
		addItemButton = (Button) findViewById(R.id.buttonAddNewItem);
		addItemButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				addSupplyKitItem();
			}
		});
	}
	
	private void addSupplyKitItem(){
		if(itemNameEdit.getText().toString().equals("") || itemNameEdit.getText().toString().equals(null) ){
			Toast toast = Toast.makeText(this, "Please fill out item name", Toast.LENGTH_SHORT);
			toast.show();
		} else if( amountOwnedEdit.getText().toString().equals("") || amountOwnedEdit.getText().toString().equals(null)  ){
			Toast toast = Toast.makeText(this, "Please fill out amount owned", Toast.LENGTH_SHORT);
			toast.show();
		} else if( amountRequiredEdit.getText().toString().equals("") || amountRequiredEdit.getText().toString().equals(null) ){
			Toast toast = Toast.makeText(this, "Please fill out amount required", Toast.LENGTH_SHORT);
			toast.show();
		}
		else{
			String expirationString;
			String perishable;
			int kitType = kitTypeSpinner.getSelectedItemPosition();
			SQLiteDatabase db = supplyDB.getWritableDatabase();
			StringBuilder sb = new StringBuilder("");
			if(perishableCheckBox.isChecked()){
				perishable = "true";
				sb.append(expirationDatePicker.getMonth()+1);
				sb.append("/");
				sb.append(expirationDatePicker.getDayOfMonth());
				sb.append("/");
				sb.append(expirationDatePicker.getYear());
				expirationString = sb.toString();
			} else{
				perishable = "false";
				expirationString = "No Expiration";
			}
			db.execSQL("INSERT INTO " + Constants.KIT_TABLE_NAME +  " (" + Constants.ITEM_NAME +", " +  
					Constants.AMOUNT_REQUIRED + ", " + Constants.AMOUNT_OWNED + ", " + Constants.PERISHABLE + ", " 
					+ Constants.EXPIRATON_DATE + ", " + Constants.KIT_TYPE_FILTER + ") " + 
					"VALUES ('" +  itemNameEdit.getText().toString() + "', '" +  amountRequiredEdit.getText().toString() + "', '" 
					+ amountOwnedEdit.getText().toString() + "', '" + perishable + "', '" + expirationString + "', '" + kitType + "');");
			db.close();
			this.finish();
		}
	}
}
